package com.lmb.todo.utils;

/**
 * @author lmc
 */
public class StringUtil {

    private static final String ALLOW_CHARS = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";

    private static final String ALLOW_SPECIAL_CHARS = "!@#$%^&*";

    private static final int USERNAME_PASSWORD_LENGTH_MIN = 8;

    private static final int USERNAME_PASSWORD_LENGTH_MAX = 18;

    /**
     * 判断用户名是否符合要求：长度在8-16之间，由大小写字母，数字组成。
     * @return boolean
     */
    public static boolean checkUsername(String username) {
        if (Constant.ADMINUSERNAME.equals(username)) {
            return true;
        }

        int len = username.length();
        if (len < USERNAME_PASSWORD_LENGTH_MIN || len > USERNAME_PASSWORD_LENGTH_MAX) {
            return false;
        }
        for (int i = 0; i < len; i++) {
            if (ALLOW_CHARS.indexOf(username.charAt(i)) == -1) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断用户密码是否符合要求：长度在8-16之间，由大小写字母，数字，特殊字符组成，至少含有一个特殊字符。
     * @return boolean
     */
    public static boolean checkPassword(String password){
        String passwordAllowChars = ALLOW_CHARS + ALLOW_SPECIAL_CHARS;
        int len = password.length();
        int isSpecialCount = 0;
        if (len < USERNAME_PASSWORD_LENGTH_MIN || len > USERNAME_PASSWORD_LENGTH_MAX) {
            return false;
        }
        for (int i = 0; i < len; i++) {
            if (passwordAllowChars.indexOf(password.charAt(i)) == -1) {
                return false;
            }
            if (ALLOW_SPECIAL_CHARS.indexOf(password.charAt(i)) != -1) {
                isSpecialCount ++;
            }
        }
        return isSpecialCount >= 1;
    }
}
